home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / code / mee / vbdao / visdata / query.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-10-06  |  18.2 KB  |  614 lines

  1. VERSION 2.00
  2. Begin Form fQuery 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Query Builder"
  6.    ClientHeight    =   5130
  7.    ClientLeft      =   1230
  8.    ClientTop       =   1155
  9.    ClientWidth     =   7095
  10.    ControlBox      =   0   'False
  11.    Height          =   5535
  12.    Icon            =   0
  13.    Left            =   1170
  14.    LinkTopic       =   "Form1"
  15.    MaxButton       =   0   'False
  16.    MDIChild        =   -1  'True
  17.    ScaleHeight     =   5112
  18.    ScaleMode       =   0  'User
  19.    ScaleWidth      =   7116
  20.    Top             =   810
  21.    Width           =   7215
  22.    Begin PictureBox ExpressionBox 
  23.       BackColor       =   &H00C0C0C0&
  24.       Height          =   1092
  25.       Left            =   120
  26.       ScaleHeight     =   1065
  27.       ScaleWidth      =   6825
  28.       TabIndex        =   17
  29.       Tag             =   "OLS"
  30.       Top             =   120
  31.       Width           =   6852
  32.       Begin CommandButton GetValuesButton 
  33.          Caption         =   "List Possible &Values"
  34.          Height          =   252
  35.          Left            =   4200
  36.          TabIndex        =   23
  37.          Top             =   720
  38.          Width           =   2292
  39.       End
  40.       Begin ComboBox cValue 
  41.          BackColor       =   &H00FFFFFF&
  42.          Height          =   288
  43.          Left            =   4080
  44.          Sorted          =   -1  'True
  45.          TabIndex        =   22
  46.          Tag             =   "POLS"
  47.          Text            =   "cValue"
  48.          Top             =   360
  49.          Width           =   2652
  50.       End
  51.       Begin ComboBox cOperator 
  52.          BackColor       =   &H00FFFFFF&
  53.          Height          =   288
  54.          Left            =   2880
  55.          Style           =   2  'Dropdown List
  56.          TabIndex        =   21
  57.          Tag             =   "POLS"
  58.          Top             =   360
  59.          Width           =   1092
  60.       End
  61.       Begin ComboBox cField 
  62.          BackColor       =   &H00FFFFFF&
  63.          Height          =   288
  64.          Left            =   120
  65.          Style           =   2  'Dropdown List
  66.          TabIndex        =   20
  67.          Tag             =   "POLS"
  68.          Top             =   360
  69.          Width           =   2652
  70.       End
  71.       Begin CommandButton ORButton 
  72.          Caption         =   "&Or into Criteria"
  73.          Height          =   252
  74.          Left            =   2040
  75.          TabIndex        =   19
  76.          Top             =   720
  77.          Width           =   1812
  78.       End
  79.       Begin CommandButton ANDButton 
  80.          Caption         =   "&And into Criteria"
  81.          Height          =   252
  82.          Left            =   120
  83.          TabIndex        =   18
  84.          Top             =   720
  85.          Width           =   1812
  86.       End
  87.       Begin Label OperatorLabel 
  88.          BackColor       =   &H00C0C0C0&
  89.          Caption         =   "Operator:"
  90.          Height          =   192
  91.          Left            =   2880
  92.          TabIndex        =   26
  93.          Top             =   120
  94.          Width           =   972
  95.       End
  96.       Begin Label ValueLabel 
  97.          BackColor       =   &H00C0C0C0&
  98.          Caption         =   "Value:"
  99.          Height          =   192
  100.          Left            =   4080
  101.          TabIndex        =   25
  102.          Top             =   120
  103.          Width           =   1452
  104.       End
  105.       Begin Label FieldNameLabel 
  106.          BackColor       =   &H00C0C0C0&
  107.          Caption         =   "Field Name:"
  108.          Height          =   192
  109.          Left            =   120
  110.          TabIndex        =   24
  111.          Top             =   120
  112.          Width           =   1332
  113.       End
  114.    End
  115.    Begin CommandButton JoinButton 
  116.       Caption         =   "Set Table &Joins"
  117.       Height          =   255
  118.       Left            =   4440
  119.       TabIndex        =   16
  120.       Top             =   2520
  121.       Width           =   2535
  122.    End
  123.    Begin ListBox cJoinFields 
  124.       BackColor       =   &H00FFFFFF&
  125.       Height          =   420
  126.       Left            =   4440
  127.       TabIndex        =   15
  128.       Tag             =   "OLS"
  129.       Top             =   2760
  130.       Width           =   2535
  131.    End
  132.    Begin CommandButton CopySQLButton 
  133.       Caption         =   "&Copy SQL"
  134.       Height          =   375
  135.       Left            =   3000
  136.       TabIndex        =   14
  137.       Top             =   4680
  138.       Width           =   1095
  139.    End
  140.    Begin ComboBox cOrderByField 
  141.       BackColor       =   &H00FFFFFF&
  142.       Height          =   300
  143.       Left            =   4440
  144.       Style           =   2  'Dropdown List
  145.       TabIndex        =   12
  146.       Tag             =   "OLS"
  147.       Top             =   2160
  148.       Width           =   2535
  149.    End
  150.    Begin ComboBox cGroupByField 
  151.       BackColor       =   &H00FFFFFF&
  152.       Height          =   300
  153.       Left            =   4440
  154.       Style           =   2  'Dropdown List
  155.       TabIndex        =   10
  156.       Tag             =   "OLS"
  157.       Top             =   1560
  158.       Width           =   2535
  159.    End
  160.    Begin ListBox cTableList 
  161.       BackColor       =   &H00FFFFFF&
  162.       Height          =   1590
  163.       Left            =   120
  164.       MultiSelect     =   1  'Simple
  165.       TabIndex        =   9
  166.       Tag             =   "OLS"
  167.       Top             =   1560
  168.       Width           =   1815
  169.    End
  170.    Begin CommandButton ShowSQLButton 
  171.       Caption         =   "&Show SQL"
  172.       Height          =   375
  173.       Left            =   1680
  174.       TabIndex        =   8
  175.       Top             =   4680
  176.       Width           =   1095
  177.    End
  178.    Begin ListBox cShowFields 
  179.       BackColor       =   &H00FFFFFF&
  180.       Height          =   1590
  181.       Left            =   2040
  182.       MultiSelect     =   1  'Simple
  183.       TabIndex        =   5
  184.       Tag             =   "OLS"
  185.       Top             =   1560
  186.       Width           =   2295
  187.    End
  188.    Begin CommandButton CloseButton 
  189.       Cancel          =   -1  'True
  190.       Caption         =   "Close"
  191.       Height          =   375
  192.       Left            =   5640
  193.       TabIndex        =   2
  194.       Top             =   4680
  195.       Width           =   1095
  196.    End
  197.    Begin CommandButton RunQueryButton 
  198.       Caption         =   "&Run Query"
  199.       Height          =   375
  200.       Left            =   360
  201.       TabIndex        =   1
  202.       Top             =   4680
  203.       Width           =   1095
  204.    End
  205.    Begin CommandButton ClearButton 
  206.       Caption         =   "C&lear All"
  207.       Height          =   375
  208.       Left            =   4320
  209.       TabIndex        =   0
  210.       Top             =   4680
  211.       Width           =   1095
  212.    End
  213.    Begin TextBox cCriteria 
  214.       BackColor       =   &H00FFFFFF&
  215.       Height          =   1215
  216.       Left            =   120
  217.       MultiLine       =   -1  'True
  218.       ScrollBars      =   2  'Vertical
  219.       TabIndex        =   3
  220.       Tag             =   "OLS"
  221.       Top             =   3360
  222.       Width           =   6855
  223.    End
  224.    Begin Label OrberByFieldLabel 
  225.       BackColor       =   &H00C0C0C0&
  226.       Caption         =   "Order By Field:"
  227.       Height          =   192
  228.       Left            =   4440
  229.       TabIndex        =   13
  230.       Top             =   1920
  231.       Width           =   2055
  232.    End
  233.    Begin Label GroupByFieldLabel 
  234.       BackColor       =   &H00C0C0C0&
  235.       Caption         =   "Group By Field:"
  236.       Height          =   192
  237.       Left            =   4440
  238.       TabIndex        =   11
  239.       Top             =   1320
  240.       Width           =   2055
  241.    End
  242.    Begin Label TableListLabel 
  243.       BackColor       =   &H00C0C0C0&
  244.       Caption         =   "Select Tables:"
  245.       Height          =   192
  246.       Left            =   120
  247.       TabIndex        =   7
  248.       Top             =   1320
  249.       Width           =   1455
  250.    End
  251.    Begin Label ShowFieldsLabel 
  252.       BackColor       =   &H00C0C0C0&
  253.       Caption         =   "Select Fields to Show:"
  254.       Height          =   195
  255.       Left            =   2040
  256.       TabIndex        =   6
  257.       Top             =   1320
  258.       Width           =   2055
  259.    End
  260.    Begin Label CriteriaLabel 
  261.       BackColor       =   &H00C0C0C0&
  262.       Caption         =   "Criteria:"
  263.       Height          =   180
  264.       Left            =   120
  265.       TabIndex        =   4
  266.       Top             =   3150
  267.       Width           =   1335
  268.    End
  269. Dim FShowSQL As Integer
  270. Dim FCopySQL As Integer
  271. Sub ANDButton_Click ()
  272.   Dim typ As Integer
  273.   Dim fn As String
  274.   Dim tb As String
  275.   If Len(cField) = 0 Then Exit Sub
  276.   tb = stSTF((cField), 0)
  277.   fn = stSTF((cField), 1)
  278.   typ = gCurrentDB.TableDefs(StripBrackets(tb)).Fields(StripBrackets(fn)).Type
  279.   If Len(cCriteria) > 0 Then
  280.     cCriteria = cCriteria & CRLF & "And "
  281.   End If
  282.   If typ = FT_STRING Or typ = FT_MEMO Or typ = FT_DATETIME Then
  283.     cCriteria = cCriteria + cField & " " & cOperator & " '" & cValue & "'"
  284.   Else
  285.     cCriteria = cCriteria + cField & " " & cOperator & " " & cValue
  286.   End If
  287.   cField.SetFocus
  288. End Sub
  289. Sub cField_Click ()
  290.   cValue.Clear
  291. End Sub
  292. Sub ClearButton_Click ()
  293.   cCriteria = NULL_STR
  294. End Sub
  295. Sub CloseButton_Click ()
  296.   Unload Me
  297. End Sub
  298. Sub CopySQLButton_Click ()
  299.   FCopySQL = True
  300.   Call RunQueryButton_Click
  301.   FCopySQL = False
  302. End Sub
  303. Sub cTableList_Click ()
  304.   Dim i As Integer, ii As Integer
  305.   Dim t As TableDef
  306.   Dim st As String
  307.   MsgBar "Updating Form Fields", True
  308.   cField.Clear
  309.   cShowFields.Clear
  310.   cGroupByField.Clear
  311.   cOrderByField.Clear
  312.   cValue.Clear
  313.   cGroupByField.AddItem "(none)"
  314.   cOrderByField.AddItem "(none)"
  315.   For ii = 0 To cTableList.ListCount - 1
  316.     If cTableList.Selected(ii) Then
  317.       Set t = gCurrentDB.TableDefs(cTableList.List(ii))
  318.       For i = 0 To t.Fields.Count - 1
  319.         st = AddBrackets((cTableList.List(ii))) & "." & AddBrackets((t.Fields(i).Name))
  320.         cField.AddItem st
  321.         cShowFields.AddItem st
  322.         cGroupByField.AddItem st
  323.         cOrderByField.AddItem st
  324.       Next
  325.     End If
  326.   Next
  327.   If Len(cField.List(0)) > 0 Then
  328.     cField.ListIndex = 0
  329.     cGroupByField.ListIndex = 0
  330.     cOrderByField.ListIndex = 0
  331.   End If
  332.   MsgBar NULL_STR, False
  333. End Sub
  334. Sub Form_Load ()
  335.    On Local Error GoTo FLErr
  336.    Dim ds As Dynaset
  337.    Dim i As Integer
  338.    Dim t As TableDef
  339.    'Clear listbox
  340.    cCriteria = NULL_STR
  341.    'Fill the Operator combo
  342.    cOperator.AddItem "="
  343.    cOperator.AddItem "<>"
  344.    cOperator.AddItem ">"
  345.    cOperator.AddItem ">="
  346.    cOperator.AddItem "<"
  347.    cOperator.AddItem "<="
  348.    cOperator.AddItem "Like"
  349.    cOperator.ListIndex = 0
  350.    'fill the table list
  351.    For i = 0 To fTables.cTableList.ListCount - 1
  352.      cTableList.AddItem StripOwner((fTables.cTableList.List(i)))
  353.    Next
  354.    cTableList.ListIndex = 0
  355.    cValue = NULL_STR
  356.   GoTo FLEnd
  357. FLErr:
  358.   ShowError
  359.   Resume FLEnd
  360. FLEnd:
  361.   Height = 5520
  362.   Width = 7224
  363.   Left = (VDMDI.Width - Width) / 2
  364.   Top = 0
  365. End Sub
  366. Sub Form_Paint ()
  367.   Outlines Me
  368.   PicOutlines ExpressionBox, cField
  369.   PicOutlines ExpressionBox, cOperator
  370.   PicOutlines ExpressionBox, cValue
  371. End Sub
  372. Sub Form_Resize ()
  373.   On Error Resume Next
  374.   If WindowState <> 1 Then
  375.     Height = 5520
  376.     Width = 7224
  377.   End If
  378. End Sub
  379. Sub GetValuesButton_Click ()
  380.   Dim ds As Dynaset
  381.   On Error GoTo GVErr
  382.   MsgBar "Getting Possible Values", True
  383.   SetHourglass Me
  384.   Set ds = gCurrentDB.CreateDynaset("select Distinct " & cField & " from " & stSTF((cField), 0))
  385.   Do While ds.EOF = False
  386.     If Len(Trim(ds(0))) > 0 Then
  387.       cValue.AddItem ds(0).Value
  388.     End If
  389.     ds.MoveNext
  390.   Loop
  391.   ds.Close
  392.   cValue.AddItem "_P1_"
  393.   cValue.AddItem "_P2_"
  394.   cValue.AddItem "_P3_"
  395.   cValue.AddItem "_P4_"
  396.   cValue = cValue.List(0)
  397.   cValue.SetFocus
  398.   GoTo GVEnd
  399. GVErr:
  400.   cValue = NULL_STR
  401.   Resume GVEnd
  402. GVEnd:
  403.   ResetMouse Me
  404.   MsgBar NULL_STR, False
  405. End Sub
  406. Sub JoinButton_Click ()
  407.   Dim i As Integer
  408.   Dim c As Integer
  409.   For i = 0 To cTableList.ListCount - 1
  410.     If cTableList.Selected(i) = True Then
  411.       c = c + 1
  412.     End If
  413.   Next
  414.   If c < 2 Then
  415.     Beep
  416.     MsgBox "You Must Have at Least 2 Tables Selected!", 48
  417.   Else
  418.     MsgBar "Choose Joins", False
  419.     fJoin.Show MODAL
  420.     MsgBar NULL_STR, False
  421.   End If
  422. End Sub
  423. Sub ORButton_Click ()
  424.   Dim typ As Integer
  425.   Dim fn As String
  426.   Dim tb As String
  427.   If Len(cField) = 0 Then Exit Sub
  428.   tb = stSTF((cField), 0)
  429.   fn = stSTF((cField), 1)
  430.   typ = gCurrentDB.TableDefs(StripBrackets(tb)).Fields(StripBrackets(fn)).Type
  431.   If Len(cCriteria) > 0 Then
  432.     cCriteria = cCriteria & CRLF & " Or "
  433.   End If
  434.   If typ = FT_STRING Or typ = FT_MEMO Or typ = FT_DATETIME Then
  435.     cCriteria = cCriteria + cField & " " & cOperator & " '" & cValue & "'"
  436.   Else
  437.     cCriteria = cCriteria + cField & " " & cOperator & " " & cValue
  438.   End If
  439.   cField.SetFocus
  440. End Sub
  441. Sub RunQueryButton_Click ()
  442.   On Error GoTo okerr
  443.      Dim ds As Dynaset
  444.      Dim fs As String
  445.      Dim ts As String
  446.      Dim i As Integer
  447.     MsgBar "Building Query", True
  448.      If Len(cCriteria) > 0 Then
  449.        stWhere$ = "AND " & LTrim(cCriteria)
  450.        'strip CRLFs
  451.        For i = 1 To Len(stWhere$)
  452.          If Mid(stWhere$, i, 1) = Chr$(13) Then
  453.            stTmp$ = stTmp$ & " "
  454.          ElseIf Mid(stWhere$, i, 1) = Chr$(10) Then
  455.            'do nothing
  456.          Else
  457.            stTmp$ = stTmp$ + Mid(stWhere$, i, 1)
  458.          End If
  459.        Next
  460.        stWhere$ = stTmp$
  461.        stWhere$ = RTrim(stWhere$)
  462.      
  463.        'Add parens to stWhere$
  464.         stTmpWhere$ = stWhere$
  465.         Do
  466.           stTmp$ = stGetToken(stTmpWhere$, " ")
  467.           stTmp$ = stTmp$ & " "
  468.            If fMatchParen% = False And UCase(stTmp$) = "AND " Then
  469.             stNewWhere$ = stNewWhere$ + stTmp$ & "("
  470.             fMatchParen% = True
  471.           ElseIf fMatchParen% = True And UCase(stTmp$) = "AND " Then
  472.             stNewWhere$ = stNewWhere$ & ") " & stTmp$ & "("
  473.             'fMatchParen% = False
  474.           Else
  475.             If UCase(stTmp$) = "OR" Or UCase(stTmp$) = "IN " Or UCase(stTmp$) = "LIKE" Then
  476.               stNewWhere$ = stNewWhere$ & " " & stTmp$
  477.             Else
  478.               stNewWhere$ = stNewWhere$ + stTmp$
  479.             End If
  480.           End If
  481.         Loop Until stTmpWhere$ = NULL_STR
  482.         stWhere$ = stNewWhere$ & ")"
  483.        'Build DynaSet string:
  484.        'Peel off leading AND/OR
  485.        If Mid(stWhere$, 2, 2) = "OR" Then
  486.          stWhere$ = Mid(stWhere$, 5, Len(stWhere$) - 5)
  487.        Else
  488.          stTmp$ = stGetToken(stWhere$, " ")
  489.        End If
  490.        If Len(stWhere$) > 0 Then
  491.          stWhere$ = " Where " & stWhere$
  492.        End If
  493.      End If
  494.      'check for join condition
  495.      If cJoinFields.ListCount > 0 Then
  496.        If Len(stWhere$) = 0 Then
  497.          stWhere$ = stWhere$ & " Where "
  498.        Else
  499.          stWhere$ = stWhere$ & " And "
  500.        End If
  501.        For i = 0 To cJoinFields.ListCount - 1
  502.          stWhere$ = stWhere$ + cJoinFields.List(i) & " And "
  503.        Next
  504.        stWhere$ = Mid(stWhere$, 1, Len(stWhere$) - 5)
  505.      End If
  506.      
  507.      'check for group by field
  508.      If cGroupByField <> "(none)" Then
  509.        stWhere$ = stWhere$ & " Group By " & cGroupByField
  510.      End If
  511.      'check for order by field
  512.      If cOrderByField <> "(none)" Then
  513.        stWhere$ = stWhere$ & " Order By " & cOrderByField
  514.      End If
  515.      'get show field names
  516.      For i% = 0 To cShowFields.ListCount - 1
  517.        If cShowFields.Selected(i%) Then
  518.          fs = fs + cShowFields.List(i%) & ","
  519.        End If
  520.      Next
  521.      If Len(fs) = 0 Then
  522.        For i% = 0 To cTableList.ListCount - 1
  523.          If cTableList.Selected(i%) Then
  524.            fs = fs + AddBrackets((cTableList.List(i%))) & ".*,"
  525.          End If
  526.        Next
  527.        If Len(fs) = 0 Then
  528.          fs = "*"
  529.        Else
  530.          fs = Mid(fs, 1, Len(fs) - 1)     'take off the last ","
  531.        End If
  532.      Else
  533.        fs = Mid(fs, 1, Len(fs) - 1)
  534.      End If
  535.      'get table names
  536.      For i% = 0 To cTableList.ListCount - 1
  537.        If cTableList.Selected(i%) Then
  538.          ts = ts + AddBrackets((cTableList.List(i%))) & ","
  539.        End If
  540.      Next
  541.      ts = Mid(ts, 1, Len(ts) - 1)
  542.      gstDynaString = "Select " & fs & " From " & ts + stWhere$
  543.          
  544.      If FShowSQL = False And FCopySQL = False Then
  545.        MsgBar "Running Query", True
  546.        gfFromSQL = True
  547.        'create a new dynaset form
  548.        If VDMDI.cSingleRecord = True Then
  549.          Dim dsform1 As New fDynaset
  550.          dsform1.Show
  551.        Else
  552.          Dim dsform2 As New fGridFrm
  553.          dsform2.Show
  554.        End If
  555.      ElseIf FShowSQL = True Then
  556.        MsgBar NULL_STR, False
  557.        MsgBox gstDynaString, 0, "SQL Query"
  558.      ElseIf FCopySQL = True Then
  559.        fSQL.cSQLStatement = gstDynaString
  560.      End If
  561.   GoTo OKEnd
  562. okerr:
  563.   If Err = 364 Then Resume OKEnd   'catch unloaded form
  564.   ShowError
  565.   Resume OKEnd
  566. OKEnd:
  567.   MsgBar NULL_STR, False
  568. End Sub
  569. Sub ShowSQLButton_Click ()
  570.   FShowSQL = True
  571.   Call RunQueryButton_Click
  572.   FShowSQL = False
  573. End Sub
  574. Function stGetToken (stLn$, stDelim$) As String
  575.     On Error GoTo GetTokenError
  576.     iOpenQuote% = InStr(1, stLn$, """")
  577.     iDelim% = InStr(1, stLn$, stDelim$)
  578.     If (iOpenQuote% > 0) And (iOpenQuote% < iDelim%) Then
  579.          iCloseQuote% = InStr(iOpenQuote% + 1, stLn$, """")
  580.          iDelim% = InStr(iCloseQuote% + 1, stLn$, stDelim$)
  581.     End If
  582.     If (iDelim% <> 0) Then
  583.          stToken$ = LTrim$(RTrim$(Mid$(stLn$, 1, iDelim% - 1)))
  584.          stLn$ = Mid$(stLn$, iDelim% + 1)
  585.     Else
  586.          stToken$ = LTrim$(RTrim$(Mid$(stLn$, 1)))
  587.          stLn$ = NULL_STR
  588.     End If
  589.     If (Len(stToken$) > 0) Then
  590.          If (Mid$(stToken$, 1, 1) = """") Then
  591.               stToken$ = Mid$(stToken$, 2)
  592.          End If
  593.          If (Mid$(stToken$, Len(stToken$), 1) = """") Then
  594.               stToken$ = Mid$(stToken$, 1, Len(stToken$) - 1)
  595.          End If
  596.     End If
  597.     stGetToken = stToken$
  598. GetTokenExit:
  599.     Exit Function
  600. GetTokenError:
  601.     Resume GetTokenExit
  602. End Function
  603. 'function to split the table and the field from a tbl.fld pair
  604. Function stSTF (tf As String, part As Integer) As String
  605.   If InStr(InStr(1, tf, ".") + 1, tf, ".") > 1 Then
  606.     tf = StripOwner(tf)
  607.   End If
  608.   If part = 0 Then
  609.     stSTF = Mid(tf, 1, InStr(1, tf, ".") - 1)
  610.   Else
  611.     stSTF = Mid(tf, InStr(1, tf, ".") + 1, Len(tf))
  612.   End If
  613. End Function
  614.